<template>
	<view class="container">
		<view class="title">
			<u-icon size="20px" name="arrow-left" @click="goBack"></u-icon>
			<text>{{getRinseInfo.size.title}} {{getRinseInfo.paper.title}}</text>
		</view>
		<view class="content" :style="{height: contentHeight}">
			<scroll-view style="height: 100%" scroll-y>
				<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" width="110px" height="110px"
					multiple :maxCount="10" uploadText="添加照片">
					<u-button type="error" shape="circle" text="+ 添加照片"></u-button>
				</u-upload>
				<text style="font-size: 12px;" v-if="!fileList.length">请选择您想要打印的照片~</text>
			</scroll-view>
		</view>
		<view class="footer">
			<view>
				<view class="counter">共{{ fileList.length }}张，￥{{ Math.round(fileList.length * getRinseInfo.price * 100) / 100 }}</view>
				<view class="buy">
					<u-button type="warning" :hairline="true" shape text="立即购买" @click="buy" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import {
		upload
	} from '@/utils/request.js'
	export default {
		created() {},
		data() {
			return {
				fileList: [],
				contentHeight: '520px'
			}
		},
		mounted() {
			let that = this;
			uni.getSystemInfo({
				success: resu => {
					// resu 可以获取当前屏幕的高度
					const query = uni.createSelectorQuery();
					query.select('.content').boundingClientRect();
					query.exec(res => {
						that.contentHeight = (resu.windowHeight - res[0].top - 80) + 'px';
					});
				},
				fail: res => {}
			});
			this.fileList = this.getPhotoList;
		},
		computed: {
			...mapGetters('rinse', ['getRinseInfo', 'getPhotoList']),
		},
		methods: {
			...mapMutations('rinse', ['setPhotoList']),
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.setPhotoList(this.fileList)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await upload(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				this.setPhotoList(this.fileList)
			},
			buy() {
				uni.navigateTo({
					url: '/Self-rinse/rinse/order'
				})
			},
			goBack() {
				wx.navigateBack(1)
			}
		}

	}
</script>

<style lang="scss" scoped>
	.container {
		margin-top: var(--status-bar-height);

		.title {
			padding: 10px 0;
			border-bottom: 1px solid;
			display: flex;
			align-items: center;

			text {
				margin-left: 10px;
			}
		}

		.content {
			padding: 10px;
			text-align: center;

			::v-deep .u-upload {
				align-items: center;
			}
		}

		.footer {
			height: 60px;

			&>view {
				position: fixed;
				bottom: 0;
				width: 95%;
				padding: 10px;
				border-top: 1px solid;
				display: flex;
				justify-content: space-between;

				.counter {
					line-height: 40px;
				}

				.buy {
					width: 100px;
				}
			}
		}
	}
</style>